<template>
  <div
    id="chart4"
    :style="{ height: 2.45 * $store.state.radomRate + 'rem' }"
  ></div>
</template>
<script>
export default {
  data() {
    return {
      time: new Date().getTime(),
    };
  },
  mounted() {
    this.communityRank();
  },
  methods: {
    communityRank() {
      let params = {
        time: this.time,
      };
      let result = this.$md5.transferValue(params);
      this.axios
        .post(this.stateUrl.dataModel.communityRank, {
          time: this.time,
          sign: this.$md5.val(result),
        })
        .then((res) => {
          this.drawLine(res.data.data);
        })
        .catch(() => {});
    },
    drawLine(list) {
      let that = this;
      let myChart = this.$echarts.init(document.getElementById("chart4"));
      let listData = list.splice(0,5);
      let XData = listData.map(item=>{
        return item.communityName
      })
     
      let YData = listData.map(item=>{
        return item.count
      })
      var option = {
        tooltip: {
          trigger: "axis",
          backgroundColor: "rgba(0,0,0,0.7)", //通过设置rgba调节背景颜色与透明度
          borderWidth: "0",
          textStyle: {
            color: "#fff",
          },
        grid: {
          left: "5%",
          top: "15%",
          bottom: "15%",
          right: "5%",
        },
          formatter: function (params) {
            return `${params[0].name}<br/>${params[0].value}`;
          },
        },
        title: {
          x: "center",
          y: "4%",
          textStyle: {
            color: "#fff",
            fontSize: "22",
          },
          subtextStyle: {
            color: "#90979c",
            fontSize: "16",
          },
        },
         grid: {
          left: "6%",
          top: "15%",
          bottom: "15%",
          right: "5%",
        },
        xAxis: [
          {
            type: "category",
            data: XData,
            axisLine: {
              lineStyle: {
                color: "rgba(255,255,255,0.12)",
              },
            },
            axisLabel: {
              margin: 10,
              color: "#e2e9ff",
              interval: 0,
              textStyle: {
                fontSize: 14,
              },
            },
          },
        ],
        yAxis: [
          {
            name: "",
            axisLabel: {
              formatter: "{value}",
              color: "#e2e9ff",
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: "rgba(255,255,255,1)",
              },
            },
            splitLine: {
              lineStyle: {
                color: "rgba(255,255,255,0.12)",
              },
            },
          },
        ],
        series: [
          {
            type: "bar",
            data: YData,
            barWidth: '28%',
            itemStyle: {
              normal: {
                color: new this.$echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: "rgba(252, 86, 89, 1)", // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: "rgba(86, 38, 209, 1)", // 100% 处的颜色
                    },
                  ],
                  false
                ),
                barBorderRadius: [5, 5, 5, 5],
              },
            },
            label: {
              normal: {
                show: true,
                color:'#fff',
                formatter:  (para)=>{
                  return para.value
            },
              },
            },
          },
        ],
      };
      myChart.setOption(option);
      myChart.on('click',function(){
        that.$router.push("/adviceList");
      })
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    },
  },
};
</script>